import { queryList } from "@/api/cabin/vesselData";

const ship = {
  state: {
    sysType: "shore", // ship 船端 shore 岸端
    currentMMSI: "",
    shipList: [],
  },
  mutations: {
    SET_MMSI: (state, MMSI) => {
      state.currentMMSI = MMSI
    },
    SET_SHIPLIST: (state, ilst) => {
      state.shipList = ilst
    },
  },
  actions: {
    getShipList({ commit }) {
      return new Promise((resolve, reject) => {
        if (this.state.ship.currentMMSI) {
          resolve()
        } else {
          queryList().then(({ data }) => {
            if (data.length > 0) {
              commit("SET_SHIPLIST", data);
              commit("SET_MMSI", data[0].mmsi);
            }
            resolve()
          }).catch(error => {
            reject(error)
          });
        }
      })
    }
  }
}

export default ship
